/**
 * 工具类  都是静态方法
 */

define([],function(){
	'use strict';
	
	var U = {};
	// 判断是否为空
	U.isEmpty = function(obj){
		if(typeof(obj)=='undefined' || obj==null || obj=='undefined' || (typeof(obj)=='string' && obj=="")){
			return true;
		}
		return false;
	}
	
	// 判断是否是function
	U.isFun = function(obj){
		if(obj && obj instanceof Function){
			return true;
		}
		return false;
	}
	
	// 判断是否是数字
	U.isNum = function(obj){
		if( (typeof(obj) === "number" || typeof(obj) === "string") && !isNaN(obj - parseFloat(obj)) ){
			return true;
		}
		return false;
	}
	
	U.isArray = function(obj){
		return Array.isArray(obj);
	}
	
	// 根据参数名获取url的参数值
	U.gerUrlParamValue = function(paramName){
		var url = window.location.href;
		if(url.indexOf('?')!=-1){
			var url_param = url.split('?')[1]; // 获取参数部分
			var param_item_array = url_param.split('&'); // 获取参数数组
			for(var i=0; i<param_item_array.length; i++){
				var name = param_item_array[i].split('=')[0];
				var value = param_item_array[i].split('=')[1];
				if(name == paramName){
					return value; // 返回参数值
				}
			}
		}
		return '';
	}
	
	// 构造一个标准的form表单进行post提交 且页面跳转
	U.StandardPost = function(url,args){
	    var $form = $("<form method='post'></form>");
	    var $input;
        $form.attr({"action":url});
        $.each(args,function(key,value){console.log('key:'+key+' value:'+value)
            $input = $("<input type='hidden'>");
            $input.attr({"name":key});
            $input.val(value);
            $form.append($input);
        });
        $form.appendTo($('body'));
        $form.submit();
        $form.remove();
    }
	
	// 倒计时
	U.exact_count_down = function(end_time_string, change_call, stop_call){
		//var s = "2017-12-15 09:41:30";
		var data = {};
		var end_time = new Date(Date.parse(end_time_string.replace(/-/g, "/")));
		function start(){
			 var distance = end_time - new Date(); // 计算剩余的毫秒数
			 var days = parseInt(distance / 1000 / 60 / 60 / 24 , 10); // 计算剩余的天数
			 var hours = parseInt(distance / 1000 / 60 / 60 % 24 , 10); // 计算剩余的小时
			 var minutes = parseInt(distance / 1000 / 60 % 60, 10);// 计算剩余的分钟
			 var seconds = parseInt(distance / 1000 % 60, 10);// 计算剩余的秒数
			 days = checkTime(days); 
			 hours = checkTime(hours); 
			 minutes = checkTime(minutes); 
			 seconds = checkTime(seconds);
			 data = {
					 days:days,
					 hours:hours,
					 minutes:minutes,
					 seconds:seconds
			 }
			 if(days == '00' && hours == '00' && minutes == '00' && seconds == '00'){
				 window.clearInterval(timer);
				 if(U.isFun(stop_call)){
					 stop_call(data);
				 }
			 }
			 if(U.isFun(change_call)){
				 change_call(data);
			 }
			 //var html = days+"天" + hours+"时" + minutes+"分"+seconds+"秒"; console.log(html);
		}
		function checkTime(i){ // 将0-9的数字前面加上0，例1变为01
			if(i<10){ 
				i = "0" + i; 
			} 
			return i; 
		} 
		var timer = window.setInterval(start,1000);
		
		return {
			stop:function(){
				window.clearInterval(timer);
				if(U.isFun(stop_call)){
					 stop_call(data);
				}
			},
			getData:function(){
				return data;
			}
		}
		
		
		return {
			
		}
	}
	
	U.renderTemplate = function(template,data){
		// 如果是数组的话循环输出模板
		var matchs = template.match(/\{[\w.]+\}/g);
		if(Array.isArray(data)){
			var str = '';
			for(var i=0; i<data.length; i++){
				var obj = data[i];
				var temp = template;
				for(var j = 0 ; j < matchs.length ;j++){  
		            var name = matchs[j].replace(/[\{\}]/gi,"");  
		            var value = '';
		            try{
		            	value = eval('obj.'+name);
		            }catch (e) {
		            	console.log(e.message);
					}
		            temp = temp.replace(matchs[j], U.isEmpty(value)?'':value);  
		        }
				str += temp;
			}
			return str;
		}else if(typeof data == 'object'){
			var obj = data;
			var temp = template;
			if(temp == ""){ temp = template; }
			for(var j = 0 ; j < matchs.length ;j++){
	            var name = matchs[j].replace(/[\{\}]/gi,"");
	            var value = '';
	            try{
	            	value = eval('obj.'+name);
	            }catch (e) {
					console.log(e.message);
				}
	            temp = temp.replace(matchs[j], U.isEmpty(value)?'':value);  
	        }
			return temp;
		}else{
			throw new Error('数据对象有误，必须保证是一个数组或者对象');
		}
	}
	
	/**
	 * 移除数组中的空值
	 */
	U.removeEmpty = function(array){
		if(!U.isArray(array)){
			throw Error('必须传入一个数组对象');
		}
		var array_new = [];
		for(var i=0; i<array.length; i++){
			if(!U.isEmpty(array[i])){
				array_new.push(array[i]);
			}
		}
		return array_new;
	}
	
	// 数组相关
	U.Array = {};
	// 多个数组取交集
	U.Array.intersect = function(){
		var result = new Array();
        var obj = {};
        for(var i = 0; i < arguments.length; i++){
            for(var j = 0; j < arguments[i].length; j++){
                var str = arguments[i][j];
                if(!obj[str]){
                    obj[str] = 1;
                }else{
                    obj[str]++;
                    if(obj[str] == arguments.length){
                        result.push(str);
                    }
                }
            }
        }
        return result;
	}
	
	// 多个数组取并集
	U.Array.union = function(){
		var result = new Array();
        var obj = {};
        for(var i = 0; i < arguments.length; i++){
            for(var j = 0; j < arguments[i].length; j++){
                var str=arguments[i][j];
                if(!obj[str]){
                    obj[str] = 1;
                    result.push(str);
                }
            }
        }
        return result;
	}
	
	// 单个数组去重
	U.Array.unique = function(array){
		var result = new Array();
		var tmp = {};
	    for(var i = 0; i < array.length; i++){
	        if(!tmp[array[i]]) {
	            tmp[array[i]] = 1;
	            result.push(array[i]);
	        }
	    }
		return result;
	}
	
	// 两个数组的差集  arr1 - arr2
	U.Array.minus = function(arr1, arr2){
		var result = new Array();
        var obj = {};
        for(var i = 0; i < arr2.length; i++) {
            obj[arr2[i]] = 1;
        }
        for(var j = 0; j < arr1.length; j++) {
            if(!obj[arr1[j]]){
                obj[arr1[j]] = 1;
                result.push(arr1[j]);
            }
        }
        return result;
	}
	
	// 判断数组中是否含有某个值
	U.Array.contains = function(array, val){
		for(var i=0; i < array.length; i++){
			if(array[i] == val){
				return true;
			}
		}
		return false;
	}
	
	// 数组去除空元素
	U.Array.clearEmptyElement = function(array){
		var result = array.filter(function(obj){
			return !U.isEmpty(obj);
		});
		return result;
	}
	
	// map 相关
	U.Map = {};
	// 判断map是否包含某个key
	U.Map.hasKey = function(map, key){
		for(var k in map){
			if(map.hasOwnProperty(k)){
				if(k == key){
					return true;
				}
			}
		}
		return false;
	}
	
	// 判断 map 是否含有某个value
	U.Map.hasValue = function(map, value){
		for(var k in map){
			if(map.hasOwnProperty(k)){
				if(map[k] == value){
					return true;
				}
			}
		}
		return false;
	}
	
	// 给map添加值对
	U.Map.put = function(map, key, value){
		if(typeof map != 'object'){
			throw Error('map 必须是一个对象');
		}
		map[key] = value;
		return map;
	}
	// 给map添加所有的值对
	U.Map.put = function(map, map_new){
		if(typeof map != 'object' || typeof map_new != 'object'){
			throw Error('参数必须是对象类型');
		}
		for(var k in map_new){
			if(map_new.hasOwnProperty(k)){
				map[k] = map_new[k];
			}
		}
		return map;
	}
	
	
	U.toUtf8 = function(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }
	
	return U;
});